using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.IO;
using FinWeb3.Models;
using FinWeb3.Models.ModelView;
using FinWeb3.Models.Repository;

namespace FinWeb3.Controllers
{
    [HandleError]
    [Authorize(Roles = "finwebuser")]
    public class StatementController : Controller
    {
        private BankManager bankManager;
        private BillManager billManager;
        private StatementManager statementManager;

        public StatementController()
        {
            SetupManagers();
        }

        private void SetupManagers()
        {
            if (User != null)
            {
                var user = User.Identity;
                var billRepository = new BillRepository();
                var bankRepository = new BankRepository();
                var groupRepository = new GroupRepository();
                var statementRepository = new StatementRepository();

                bankManager = new BankManager(bankRepository, user);
                billManager = new BillManager(billRepository, groupRepository, user);
                statementManager = new StatementManager(statementRepository, bankRepository, user);
            }
        }

        public ActionResult Index(string fromdate, string todate)
        {
            SetupManagers();

            var details = new AccountInfoModelView(fromdate, todate);
            details.Banks = bankManager.GetAll();
            details.Statements = statementManager.Get(details.FromDate, details.ToDate);
            details.StatementManager = statementManager;

            if (TempData.ContainsKey("msg"))
                details.Message = TempData["msg"].ToString();

            return View(details);
        }

        public ActionResult Upload(string fromdate, string todate)
        {
            if (Request.Files.Count == 0)
            {
                var details = new AccountInfoModelView(fromdate, todate);

                return View(details);
            }
            else
            {
                SetupManagers();

                StreamReader reader = new StreamReader(Request.Files[0].InputStream);
                var count = statementManager.Save(reader);

                if (count == 0)
                {
                    TempData["msg"] = "Nenhuma linha nova foi adicionada.";
                }
                else if (count == 1)
                {
                    TempData["msg"] = "Uma linha nova foi adicionada.";
                }
                else
                {
                    TempData["msg"] = string.Format("Foram adicionadas {0} linhas novas.", count);
                }

                return RedirectToAction("Index");
            }
        }

        public ActionResult CorrectTotalAmount(string fromdate, string todate, int id)
        {
            SetupManagers();

            var details = new StatementAdjustTotalModelView(fromdate, todate);
            details.Bank = bankManager.Get(id);
            details.StatementManager = statementManager;

            return View(details);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CorrectTotalAmount(string fromdate, string todate, int bankId, decimal realValue)
        {
            SetupManagers();

            statementManager.AdjustStatementTotal(bankId, realValue);

            return RedirectToAction("Index", new { fromdate = fromdate, todate = todate });
        }

        public ActionResult AssociateStatementToBill(string fromdate, string todate, int id)
        {
            SetupManagers();

            var details = new AccountInfoModelView(fromdate, todate);

            details.Banks = new List<Bank>();
            details.Banks.Add(bankManager.Get(id));
            details.Statements = statementManager.Get(id, details.FromDate, details.ToDate);
            details.Bills = billManager.Get(details.FromDate, details.ToDate, BillList.NotAssociated).ToList();

            if (TempData.ContainsKey("msg"))
                details.Message = TempData["msg"].ToString();

            return View(details);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AssociateStatementToBill(string fromdate, string todate, int billId, int statementId, int bankId)
        {
            SetupManagers();

            billManager.AssociateBillToStatement(statementId, billId);

            return RedirectToAction("AssociateStatementToBill", new { fromdate = fromdate, todate = todate, id = bankId });
        }
    }
}
